﻿using Abp.Collections.Extensions;
using Abp.Dependency;
using Mt.Site.Core.WorkFlows.Design.Cache;
using Mt.Site.Core.WorkFlows.Enum;
using Mt.Site.Core.WorkFlows.Process;
using Mt.Site.Core.WorkFlows.Process.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Mt.Site.Core.WorkFlows.Diy.Plugin.Impl.yglh
{
    public class AutoSubmitNode : WorkFlowNodePluginFunctionsBase, ITransientDependency
    {
        public const string Name = "WFNode.AutoSumit";

        public override string DisplayName => "自动提交";

        public override string UniqeName => Name;

        public async override Task<StateWhenSubmit> CheckDataBeforeSelelctUsersToNextNode(IWorkFlowManager workFlowManager, bool isNeedToSelectUsers)
        {
            var nextIdList = await workFlowManager.GetNextAsync();
            if (!nextIdList.IsNullOrEmpty())
            {
                var nextNode = nextIdList[0];

                //调取人员选择框的选项及默认选项
                var userIds = await workFlowManager
                    .GetUserListAsync(nextNode.Definition.NodeId, null, nextNode.Definition.ToUserFilter());
                var userIdList = userIds.DefaultSelectedUsers.Select(c => c.Id).ToList();
                if (userIdList.Count > 0)
                {
                    //不再弹 选人对话框
                    return new StateWhenSubmit
                    {
                        IsNeedToSelectUsers = false
                    };
                }
            }

            return new StateWhenSubmit
            {
                IsNeedToSelectUsers = true
            };
        }

        public override async Task<bool> DiyGoNextAsync(
            IWorkFlowManager workFlowManager, Guid nodeId, List<long> userids_next, List<long> turnRoundUserList, ActionScheme actionScheme_next, string replyMsg)
        {
            return await GoNextAutoAsync(workFlowManager, replyMsg);
        }
    }
}